package cn.com.w.simple.demo.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 自定义一个类实现我们自己的错误处理逻辑
 */
@Slf4j
public class CustomEntryPointFilter implements AuthenticationEntryPoint {


    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        httpServletResponse.setContentType("application/json;charset=utf-8");
        PrintWriter out = httpServletResponse.getWriter();
        log.error("身份认证失败:{}", e.getMessage(), e);
        // 直接提示前端认证错误
        out.write("认证错误啦!!!!!");
        out.flush();
        out.close();
    }

}
